﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;

public class UnitManager : NetworkBehaviour
{
    private Vector3 posBuilding;

    private GameObject gridGroup;
    private GameObject mousePointObj;
    private string mousePointObjName = "";
    #region Instance
    public static UnitManager instance = null;
    public void Awake()
    {
        instance = this;
    }
    #endregion

    public void CreatBarrack(ref bool CreatBarrackisActive)
    {
        if (gridGroup == null)
        {
            gridGroup = Instantiate(this.transform.GetComponent<Camp>().Grids, this.transform.GetComponent<Camp>().Grids.transform.position, this.transform.GetComponent<Camp>().Grids.transform.rotation);
        }
        gridGroup.SetActive(true);

        // 发射射线检测层级碰撞
        RaycastHit hit;
        LayerMask lmask = 1 << 9;// 地形层

        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        Physics.Raycast(ray, out hit, Mathf.Infinity, lmask.value);
        if (hit.transform != null)
        {
            mousePointObj = hit.transform.gameObject;
            // 修改选中位置的颜色
            if (mousePointObj.tag == "Grid")
            {
                if (mousePointObj.GetComponent<Plane>().HadBuild == true)
                {
                    mousePointObj.GetComponent<Renderer>().material.color = Color.red;
                    GameObject.Find(mousePointObjName).GetComponent<Renderer>().material.color = Color.white;
                }
                else
                {
                    mousePointObj.GetComponent<Renderer>().material.color = Color.green;
                    if (mousePointObj.name != mousePointObjName && mousePointObjName != "")
                    {
                        GameObject.Find(mousePointObjName).GetComponent<Renderer>().material.color = Color.white;
                    }
                    mousePointObjName = mousePointObj.name;
                }
            }
            else
            {
                if (mousePointObj.name != mousePointObjName && mousePointObjName != "")
                {
                    GameObject.Find(mousePointObjName).GetComponent<Renderer>().material.color = Color.white;
                }
            }
        }

        // 没有建筑以及在可释放范围内
        if (Input.GetMouseButtonDown(0) && mousePointObj.GetComponent<Plane>().HadBuild == false)
        {
            if (mousePointObj.tag == "Grid")
            {
                posBuilding = mousePointObj.transform.position;
                //Debug.Log(GameJudge.instance.Player1.GetComponent<Resource>().population);
                GameJudge.instance.Player1.GetComponent<Resource>().population += 5;

                SpawnManager.instance.CmdCreatbarr(posBuilding);//在所有端创建建筑

                //Resource.instance.UseCoin(new Barrack().price);
                this.GetComponent<Resource>().UseCoin(new Barrack().price);
                //TODO: 释放技能消耗金币
                mousePointObj.GetComponent<Renderer>().material.color = Color.white;
                gridGroup.SetActive(false);

                //Debug.Log("CreatBoom");
                // CmdCreatbarr(mousePos);
                // TODO: 利用网络释放技能
                CreatBarrackisActive = false;
            }
        }
        // 取消释放分支
        if (Input.GetMouseButtonDown(1))
        {
            gridGroup.SetActive(false);
            CreatBarrackisActive = false;
            return;
        }
    }

}
